package org.springframework.core.io;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 读取类路径下的资源文件
 */
public class ClassPathResource implements Resource {
    private final String path;

    public ClassPathResource(String path) {
        this.path = path;
    }

    /**
     * 通过读取类的类加载器获取资源的输入流
     * @return
     * @throws IOException
     */
    @Override
    public InputStream getInputStream() throws IOException {
        //通过类加载器读取类路径下文件 来获取输入流
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(this.path);
        if(is == null){
            throw new FileNotFoundException(this.path + " cannot be opened because it does not exist");
        }
        return is;
    }


}
